/**
 * 
 */

function remainTime() {
	var iHour, iMinute, iSecond, sHour, sMinute, sSecond, sTime;
	if (remainTimer > 0) {
		iHour = parseInt(((remainTimer / 3600)));
		if (iHour > 0) {
			sHour = iHour + ':';
		}else if(iHour <= 0) {
			sHour = '00:';
		}
		iMinute = parseInt((remainTimer / 60) % 60);
		if (iMinute > 0) {
			if (iMinute < 10) {
				sMinute = '0' + iMinute + ':';
			} else {
				sMinute = iMinute + ':';
			}
		} else if(iMinute <= 0) {
			sMinute = '00:';
		}
		iSecond = parseInt(remainTimer % 60);
		if (iSecond >= 0) {
			if (iSecond < 10) {
				sSecond = '0' + iSecond;
			} else {
				sSecond = iSecond;
			}
		}
		sTime = sHour + sMinute + sSecond;
		if (remainTimer <= 0) {
			clearTimeout(Account);
		} else {
			Account = setTimeout("remainTime()", 1000);
		}
		remainTimer = remainTimer - 1;
	} else {
		sTime = '00:00:00';
	}
	$("#remainTime").html(sTime);
}

remainTime();

$(function(){
	//买家取消订单
	$(".canelOrder").click(function(){
		var oid = $(this).attr('oid');
		$.ckConfirm({
			message:"你确定要取消订单吗？",
			ok:function() {
				$.getJSON("/?mod=student&act=order&do=cancelOrder&oid="+oid+"&rand="+Math.random(), function(ret){
					if (ret.code == 0) {
						window.location.reload();
					} else {
						$.ckAlert({message:ret.data});
					}
				});
			}
		});
		return false;
	});
	
	//确认打款
	$(".confirmPay").click(function(){
		var oid = $(this).attr('oid');
		var Box = $.ckAjaxBoxy({
			url : '/?mod=student&act=order&do=confirm&oid='+oid,
			title : "确认打款",
			width : 431,
			height : 316,
			callback:function(){
				$("#fe_dialogBox").find("a.cancel").unbind().bind('click', function(){
					Box.cancel();
				});
			}
		});
	});
	
	//评价课程
	$(".voteCourse").click(function(){
		var oid = $(this).attr('oid');
		var Box = $.ckAjaxBoxy({
			url : '/?mod=student&act=credit&do=voteCourse&oid='+oid,
			title : "评价课程",
			width : 540,
			height : 425,
			callback:function(){
				$("#fe_dialogBox").find("a.cancel").unbind().bind('click', function(){
					Box.cancel();
				});
			}
		});
	});
	
	//评价学生
	$(".voteStudent").click(function(){
		var oid = $(this).attr('oid');
		var Box = $.ckAjaxBoxy({
			url : '/?mod=seller&act=credit&do=voteStudent&oid='+oid+'&t='+Math.random(),
			title : "评价学生",
			width : 540,
			height : 420,
			callback:function(){
				$("#fe_dialogBox").find("a.cancel").unbind().bind('click', function(){
					Box.cancel();
				});
			}
		});
	});
	
	//拒绝听课
	$(".refuseOrder").click(function(){
		var oid = $(this).attr('oid');
		var cost = $(this).attr('cost');
		
		if (cost == 0) {
			//免费课程
			$.ckConfirm({
				message:"你确定要拒绝该学生继续听课吗？",
				ok:function() {
					$.getJSON("/?mod=seller&act=order&do=refuseOrder&oid="+oid+"&rand="+Math.random(), function(ret){
						if (ret.code == 0) {
							window.location.reload();
						} else {
							$.ckAlert({message:ret.data});
						}
					});
				}
			});
		} else {
			var Box = $.ckAjaxBoxy({
				url : '/?mod=seller&act=order&do=refuse&oid='+oid,
				title : "拒绝听课",
				width : 381,
				height : 283,
				callback:function(){
					$("#fe_dialogBox").find("a.cancel").unbind().bind('click', function(){
						Box.cancel();
					});
				}
			});
		}
		
		return false;
	});
	
	//付款前检查价格变更情况
	$(".payNow").click(function(){
		var oid = $(this).attr('oid');
		var url = $(this).attr('href');
		$.getJSON("/?mod=order&act=show&do=checkCost&oid="+oid+"&rand="+Math.random(), function(ret){
			if (ret.code == 1) {
				//价格发生变更
				$.ckConfirm({
					message : "该课程的价格已经变更为"+ret.data+"元，你是否继续购买？",
					width : "420",
					ok :function(){
						location.href = url;
					}
				});
			} else {
				location.href = url;
			}
		});
		return false;
	});
	
	//付款前检查价格变更情况
	$(".payNow2").click(function(){
		var oid = $(this).attr('oid');
		var url = $(this).attr('href');
		$.getJSON("/?mod=order&act=show&do=checkProductCost&oid="+oid+"&rand="+Math.random(), function(ret){
			if (ret.code == 1) {
				location.href = url;
			} else if (ret.code == 0) {
				$.ckTipsBoxy({
					type:"success", 
					width:352, 
					message:"购买成功，可以开始学习了。",
					mask:true,
					callback:function() {
						window.location.reload();
					}
				});
			} else {
				$.ckAlert({message:rs.data});
			}
		});
		return false;
	});
	
	$("#editBtn").click(function(){
		$("#contactor").val($("#contactor_show").text());
		$("#contactorInfo").val($("#contactor_info_show").text());
		$("#contactor_info").hide();
		$("#edit_contactor").show();
	});
	
	$("#edit_cancel").click(function(){
		$("#edit_contactor").hide();
		$("#contactor_info").show();
	});
	
	$("#edit_save").click(function(){
		var sid = $(this).attr('sid');
		var oid = $(this).attr('oid');
		var contactor = $("#contactor").val();
		var contactor_info = $("#contactorInfo").val();
		
		if (!contactor || !contactor_info) {
			$.ckAlert({message:"请填写联系人信息"});
			return false;
		}
		
		var contactor_len = $.trim(contactor).replace(/[^\x00-\xff]/g, '**').length;
		if (contactor_len > 16) {
			$.ckAlert({message:"联系人长度不能超过16个字符"});
			return false;
		}

		var contactor_info_len =  $.trim(contactor_info).replace(/[^\x00-\xff]/g, '**').length;
		if (contactor_info_len > 16) {
			$.ckAlert({message:"联系方式长度不能超过20个字符"});
			return false;
		}
		
		$.ajax({
			url:'/?mod=1v1&act=pay&do=contactor',
			type:'post',
			data:'sid='+sid+'&oid='+oid+'&contactor='+contactor+'&contactorInfo='+contactor_info,
			dataType:'json',
			success:function(ret) {
				if (ret.code == 0) {
					$("#contactor").val(contactor);
					$("#contactor_show").text(contactor);
					$("#contactorInfo").val(contactor_info);
					$("#contactor_info_show").text(contactor_info);
					$("#edit_contactor").hide();
					$("#contactor_info").show();
				} else {
					$.ckAlert({message:ret.data});
					return false;
				}
			}
		});
	});
});